1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.webmacro.engine;
25
26 import org.webmacro.PropertyException;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 /***
32 * A chained build context, for use in expanding macros.
33 * When called upon to evaluate a reference which is one of the macros
34 * formals, it evaluates the actual argument and returns that, otherwise
35 * it chains back to the build context.
36 */
37 public class MacroBuildContext extends BuildContext
38 {
39
40 private BuildContext chainedContext, rootContext;
41 private MacroDefinition macro;
42 private Object[] args;
43 private Map macroArgs = new HashMap();
44
45 public MacroBuildContext (MacroDefinition macro,
46 Object[] args,
47 BuildContext bc)
48 {
49 super(bc.getBroker());
50 this.chainedContext = bc;
51 this.args = args;
52 this.macro = macro;
53 String[] argNames = macro.getArgNames();
54 for (int i = 0; i < argNames.length; i++)
55 macroArgs.put(argNames[i], args[i]);
56
57 rootContext = chainedContext;
58 while (rootContext instanceof MacroBuildContext)
59 rootContext = ((MacroBuildContext) rootContext).chainedContext;
60 }
61
62 public BuildContext getRootContext ()
63 {
64 return rootContext;
65 }
66
67 public void putMacro (String name, MacroDefinition macro)
68 {
69 rootContext.putMacro(name, macro);
70 }
71
72 public MacroDefinition getMacro (String name)
73 {
74 return rootContext.getMacro(name);
75 }
76
77 public boolean containsKey (Object name)
78 {
79 return macroArgs.containsKey(name) || chainedContext.containsKey(name);
80 }
81
82 protected Object internalGet (Object name) throws PropertyException
83 {
84 if (macroArgs.containsKey(name))
85 return macroArgs.get(name);
86 else
87 {
88 return rootContext.get(name);
89 }
90 }
91 }
92